package com.book.xsp.bookparent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.book.xsp.bookparent.entity.Course;
import com.book.xsp.bookparent.entity.vo.AddCourseVo;
import com.book.xsp.bookparent.entity.vo.EditCourseVo;
import com.book.xsp.bookparent.mapper.CourseMapper;
import com.book.xsp.bookparent.service.ICourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author snake_skin
 * @since 2023-11-28
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Override
    public List<Course> getAllCourse() {
        return baseMapper.selectList(null);
    }

    @Override
    public boolean addCourse(AddCourseVo courseVO) {
        Course course = new Course();
        course.setCourseName(courseVO.getCourseName());
        course.setCourseIntroduce(courseVO.getCourseIntroduce());
        int insert = baseMapper.insert(course);
        return insert > 0;
    }

    @Override
    public boolean editCourse(EditCourseVo course) {
        // 新建课程实体，用于保存id和需要修改的属性
        Course insertCourse = new Course();
        insertCourse.setId(course.getId());
        insertCourse.setCourseName(course.getCourseName());
        insertCourse.setCourseIntroduce(course.getCourseIntroduce());
        int i = baseMapper.updateById(insertCourse);
        return i > 0;
    }

    @Override
    public boolean deleteCourse(Long id) {
        int i = baseMapper.deleteById(id);
        return i > 0;
    }

    @Override
    public Long getCourseIdByName(String courseName) {
        QueryWrapper<Course> qw = new QueryWrapper<>();
        qw.eq("course_name",courseName);
        Course course = baseMapper.selectOne(qw);
        return course.getId();
    }
}
